package com.xy.tianyu.module.picture.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.xy.tianyu.BaseActivity
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.adapter.PicHotDayDetailPagerAdapter
import com.xy.tianyu.module.picture.bean.Item
import com.xy.tianyu.module.picture.bean.ItemX
import com.xy.tianyu.module.picture.bean.PicSearchBean
import com.xy.tianyu.module.picture.fragment.PicDetailFragment
import kotlinx.android.synthetic.main.activity_pic_hot_day_detail.*
import java.io.Serializable

class PicHotDayDetailActivity : BaseActivity() {
    private lateinit var picHotDayList:ArrayList<Item>
    private lateinit var picHotDayItem :Item
    private lateinit var picSearchList:ArrayList<PicSearchBean.DataBean>
    private lateinit var picSearchItem:PicSearchBean.DataBean
    private lateinit var picHotDayXList:ArrayList<ItemX>
    private lateinit var picHotDayItemX :ItemX
    private val fragmentList = mutableListOf<Fragment>()
    private var intentType = 0

    companion object{
        private const val INTENT_TYPE = "intent_type"
        private const val INTENT_PIC_LIST = "intent_pic_list"
        private const val INTENT_PIC_ITEM = "intent_pic_item"
        fun actionStart1(context: Context,type:Int,list: ArrayList<Item>,item: Item){
            val intent = Intent(context,PicHotDayDetailActivity::class.java)
            intent.putExtra(INTENT_TYPE,type)
            intent.putParcelableArrayListExtra(INTENT_PIC_LIST,list)
            intent.putExtra(INTENT_PIC_ITEM,item)
            context.startActivity(intent)
        }

        fun actionStart2(context: Context, type:Int, list: ArrayList<PicSearchBean.DataBean>, item: PicSearchBean.DataBean){
            val intent = Intent(context,PicHotDayDetailActivity::class.java)
            intent.putExtra(INTENT_TYPE,type)
            intent.putExtra(INTENT_PIC_LIST,list as Serializable)
            intent.putExtra(INTENT_PIC_ITEM,item)
            context.startActivity(intent)
        }

        fun actionStart3(context: Context,type:Int,list: ArrayList<ItemX>,item: ItemX){
            val intent = Intent(context,PicHotDayDetailActivity::class.java)
            intent.putExtra(INTENT_TYPE,type)
            intent.putParcelableArrayListExtra(INTENT_PIC_LIST,list)
            intent.putExtra(INTENT_PIC_ITEM,item)
            context.startActivity(intent)
        }
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_pic_hot_day_detail
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this,ContextCompat.getColor(this,R.color.white))

        iv_back.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        intentType = intent.getIntExtra(INTENT_TYPE,0)
        var currentPosition = 0
        when(intentType){
            0 -> {
                picHotDayItem = intent.getParcelableExtra(INTENT_PIC_ITEM)
                picHotDayList = intent.getParcelableArrayListExtra(INTENT_PIC_LIST)
                if (!picHotDayList.isNullOrEmpty()){
                    for (i in picHotDayList.indices){
                        val item = picHotDayList[i]
                        if (item.pic_key == picHotDayItem.pic_key){
                            currentPosition = i
                        }
                        val fragment = PicDetailFragment()
                        val bundle = Bundle()
                        bundle.putParcelable(PicDetailFragment.BUNDLE_PIC_DETAIL,item)
                        bundle.putInt(PicDetailFragment.BUNDLE_TYPE,intentType)
                        fragment.arguments = bundle
                        fragmentList.add(fragment)
                    }
                }
            }
            1 -> {
                picSearchItem = intent.getSerializableExtra(INTENT_PIC_ITEM) as PicSearchBean.DataBean
                picSearchList = intent.getSerializableExtra(INTENT_PIC_LIST) as ArrayList<PicSearchBean.DataBean>

                if (!picSearchList.isNullOrEmpty()){
                    for (i in picSearchList.indices){
                        val item = picSearchList[i]
                        if (item.thumbURL == picSearchItem.thumbURL){
                            currentPosition = i
                        }
                        val fragment = PicDetailFragment()
                        val bundle = Bundle()
                        bundle.putSerializable(PicDetailFragment.BUNDLE_PIC_DETAIL,item)
                        bundle.putInt(PicDetailFragment.BUNDLE_TYPE,intentType)
                        fragment.arguments = bundle
                        fragmentList.add(fragment)
                    }

                }
            }
            else -> {
                picHotDayItemX = intent.getParcelableExtra(INTENT_PIC_ITEM)
                picHotDayXList = intent.getParcelableArrayListExtra(INTENT_PIC_LIST)
                if (!picHotDayXList.isNullOrEmpty()){
                    for (i in picHotDayXList.indices){
                        val item = picHotDayXList[i]
                        if (item.pic_key == picHotDayItemX.pic_key){
                            currentPosition = i
                        }
                        val fragment = PicDetailFragment()
                        val bundle = Bundle()
                        bundle.putParcelable(PicDetailFragment.BUNDLE_PIC_DETAIL,item)
                        bundle.putInt(PicDetailFragment.BUNDLE_TYPE,intentType)
                        fragment.arguments = bundle
                        fragmentList.add(fragment)
                    }
                }
            }
        }

        val pageAdapter = PicHotDayDetailPagerAdapter(supportFragmentManager,fragmentList)
        vp_pic_hot_day_detail.adapter = pageAdapter

        vp_pic_hot_day_detail.currentItem = currentPosition

    }
}